import Vue from "vue";
export default {
	namespaced: true, // 命名空间必须写
	state: {
		isLoadingProduct: true, // 是否加载商品详情 true-可加载 false-不可加载
		systemConfig: {}, // 系统配置
		spreadInfo: {}, // 平台配置
		categoryIndex: 0,
		toText: {}, // 转文字
		isShowToastClick: true, // 气泡框是否点击
		subTemplateIds: [], // 首页订阅消息模板id
		giftActivitiesList: [],
		isSettings: false, //微信小程序订阅消息
		agent_apply: 0,
		spreadToken: ''
	},
	getters: {
		getState(state) {
			return state;
		}
	},
	// 唯一修改state值的方法 
	// commit
	mutations: {
		// 首页保存分类下标
		setCategory(state, val) {
			state.categoryIndex = val
		},
		setSystemConfig(state, value) {
			state.systemConfig = value;
		},
		setSpreadInfo(state, value) {
			state.spreadInfo = value;
		},
		setGiftActivities(state, value) {
			state.giftActivitiesList = value;
		},
		setToText(state, value) {
			state.toText = value;
		},
		setIsShowToastClick(state, value) {
			state.isShowToastClick = value;
		},
		setsubTemplateIds(state, val) {
			state.subTemplateIds = Object.values(val)
		},
		setSettings(state, val) {
			state.isSettings = val
		},
		setAgentApply(state, val) {
			state.agent_apply = val
		},
		setSpreadToken(state, val) {
			state.spreadToken = val
		},
		setComment(state, val) {
			state.order_comment_img_number = val.systemConfig.order_comment_img_number
			state.order_comment_status = val.systemConfig.order_comment_status
		}
	},
	// 异步的操作
	actions: {
		// 获取初始化信息
		getInit({
			state,
			commit,
			dispatch,
			rootState
		}) {
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					let params = {
						user_terminal: 'weapp',
						code: loginRes.code,
						appid: Vue.prototype.$wxMinAppid,
					}
					Vue.prototype.$post("index/initData", params).then(res => {
						let data = res.data;
						let token = data.token.token_type + ' ' + data.token.access_token;
						uni.setStorageSync('token', token)
						// 存储是否申请过代理
						commit('setAgentApply', data.agent_apply);
						// 存储系统配置信息
						commit('setSystemConfig', data.systemConfig);
						// 存储平台信息
						commit('setSpreadInfo', data.spreadInfo);
						// 存储赠品
						commit('setGiftActivities', data.giftActivitiesList);
						// 获取隐私token
						commit('setSpreadToken', data.spread_token);
						// 存储转文字信息
						commit('setToText', data.toText);
						// 存储评论相关信息
						commit('setComment', data);
						// 获取二维码信息
						dispatch('getQrCodeParams');
						// 订阅消息：首页模板id
						if (data.subTemplateIds) {
							commit('setsubTemplateIds', data.subTemplateIds);
						}
						let isAgent = data.systemConfig.index_agent_show == 1 ? 'agent' :
							'notAgent'
						commit('tabbar/setIsAgent', isAgent, {
							root: true
						});
						commit('user/setIsLogin', true, {
							root: true
						});
						// 用户已登录
						commit('user/setUserType', data.token.user_type, {
							root: true
						});
						dispatch('index/getIndex', {}, {
							root: true
						});
						// 进入页面onLond
						Vue.prototype.$isResolve()
					}).catch(ex => {
						console.log(ex, 'ex')
						Vue.prototype.$errorToast(ex);
					})
				}
			})
			// #endif
		},
		// 获取二维码信息
		getQrCodeParams({
			state,
			commit,
			dispatch,
			rootState
		}) {
			var qrcode = uni.getStorageSync('qrcode_token') || '';
			if (!qrcode) { // 扫码进入
				return true;
			}
			Vue.prototype.$get("index/getQrCodeParamsV2", {
				qrcode: qrcode,
				qrcode_token: qrcode,
				// user_terminal: 'weapp',
			}).then(res => {
				// 清除缓存
				uni.removeStorageSync('qrcode_token');
				if (res.code == 200) {
					// 二维码参数存入缓存
					var qrCodeParams = res.data.params;
					for (var i in qrCodeParams) {
						if (i == 'parent_id') { // 上级用户ID
							// 存入缓存
							uni.setStorageSync('parent_id', qrCodeParams[i])
						}
					}
					if (res.data.path) {
						Vue.prototype['$' + res.data.root_type](res.data.path)
					}
				} else {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						confirmText: '我知道了',
					});
				}
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
	}
}